package com.sophimp.jumphack;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Created by chenliang on 2018/1/1.
 */
public class NextDirectionFinder {

    public static int[] buildArray(int i, int j) {
        int[] ret = {i, j};
        return ret;
    }

    public static void main(String... strings) throws IOException {
        MyPosFinder2 myPosFinder = new MyPosFinder2();
        BookFinder bookFinder = new BookFinder();
        String root = myPosFinder.getClass().getResource("/").getPath();
        System.out.println("root: " + root);
        String imgsSrc = root + "img/";
        String imgsDesc = root + "img/my_pos";
        File srcDir = new File(imgsSrc);
        System.out.println(srcDir);
        long cost = 0;
        File[] listFile = srcDir.listFiles();
        if (listFile == null || listFile.length == 0) return;
        for (File file : listFile) {
            if (!file.getName().endsWith(".png")) {
                continue;
            }
            System.out.println(file);
            BufferedImage img = ImgLoader.load(file.getAbsolutePath());
            long t1 = System.nanoTime();
            int[] myPos, circlePos;
            myPos = myPosFinder.find(img);

            long t2 = System.nanoTime();
            cost += (t2 - t1);
            BufferedImage desc = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics graphics = desc.getGraphics();
            graphics.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null); // 绘制缩小后的图

            // 画人物位置
            graphics.setColor(Color.RED);
            graphics.fillRect(myPos[0] - 5, myPos[1] - 5, 10, 10);

            circlePos = bookFinder.find(img, myPos);

            graphics.setColor(Color.BLUE);
            graphics.fillRect(circlePos[0] - 5, circlePos[1] - 5, 10, 10);
            if (circlePos[0] > myPos[0]){
                // 右
            }else {
                // 左
            }

            File descFile = new File(imgsDesc, file.getName());
            if (!descFile.exists()) {
                descFile.mkdirs();
                descFile.createNewFile();
            }
            ImageIO.write(desc, "png", descFile);
        }
        System.out.println("avg time cost: " + (cost / listFile.length / 1_000_000));
    }

}
